using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Protobuf;

/// <summary>
/// Protobuf使用示例
/// </summary>
public class ProtobufExample : MonoBehaviour
{
    void Start()
    {
        // 确保ProtobufNetworkSimulator存在
        if (ProtobufNetworkSimulator.Instance == null)
        {
            gameObject.AddComponent<ProtobufNetworkSimulator>();
        }

        // 创建登录请求
        var loginRequest = new LoginRequest
        {
            name = "player1",
            pass = "123456",
            deviceId = "device_001",
            platform = "Android"
        };

        // 发送登录请求
        ProtobufNetworkSimulator.Instance.SendLoginRequest(loginRequest, OnLoginResponse);
    }

    /// <summary>
    /// 登录响应处理
    /// </summary>
    /// <param name="response">登录响应</param>
    private void OnLoginResponse(LoginResponse response)
    {
        if (response.code == 0)
        {
            Debug.Log("登录成功!");
            Debug.Log($"欢迎, {response.playerInfo.nickname} (等级: {response.playerInfo.level})");
            Debug.Log($"金币: {response.playerInfo.gold}, 钻石: {response.playerInfo.diamond}");
            Debug.Log($"登录令牌: {response.token}");

            // 显示道具信息
            if (response.playerInfo.items.Count > 0)
            {
                Debug.Log("道具列表:");
                foreach (var item in response.playerInfo.items)
                {
                    Debug.Log($"  - 道具ID: {item.itemId}, 数量: {item.count}");
                }
            }

            // 显示英雄信息
            if (response.playerInfo.heroes.Count > 0)
            {
                Debug.Log("英雄列表:");
                foreach (var hero in response.playerInfo.heroes)
                {
                    Debug.Log($"  - 英雄ID: {hero.heroId}, 等级: {hero.level}, 星级: {hero.star}");
                }
            }
        }
        else
        {
            Debug.LogError($"登录失败: {response.message} (错误码: {response.code})");
        }
    }

    void Update()
    {
        // 按空格键发送另一个登录请求
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var loginRequest = new LoginRequest
            {
                name = "player2",
                pass = "123456",
                deviceId = "device_002",
                platform = "iOS"
            };

            ProtobufNetworkSimulator.Instance.SendLoginRequest(loginRequest, OnLoginResponse);
        }

        // 按回车键测试错误登录
        if (Input.GetKeyDown(KeyCode.Return))
        {
            var loginRequest = new LoginRequest
            {
                name = "unknown",
                pass = "wrong",
                deviceId = "device_003",
                platform = "PC"
            };

            ProtobufNetworkSimulator.Instance.SendLoginRequest(loginRequest, OnLoginResponse);
        }

        // 按Tab键测试序列化和反序列化
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            TestSerialization();
        }
    }

    /// <summary>
    /// 测试序列化和反序列化
    /// </summary>
    private void TestSerialization()
    {
        // 创建测试对象
        var loginRequest = new LoginRequest
        {
            name = "testUser",
            pass = "testPass",
            deviceId = "testDevice",
            platform = "testPlatform"
        };

        // 序列化
        byte[] data = ProtobufNetworkSimulator.Instance.Serialize(loginRequest);
        Debug.Log($"序列化后的数据长度: {data.Length} 字节");

        // 反序列化
        var deserializedRequest = ProtobufNetworkSimulator.Instance.Deserialize<LoginRequest>(data);
        Debug.Log($"反序列化结果: 用户名={deserializedRequest.name}, 密码={deserializedRequest.pass}");
    }
}